/**
 * @author Matthew.He
 */
package com.iot.ic.modules.dao.impl;

import com.iot.ic.common.dao.BaseDaoImpl;
import com.iot.ic.common.utils.StringUtils;
import com.iot.ic.modules.dao.UserDao;
import com.iot.ic.modules.model.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;

/**
 * @author haojinyu
 */
@Service("userDao")
public class UserDaoImpl extends BaseDaoImpl implements UserDao {

	@Override
	public User findByMobile(String mobile, int userType) throws Exception {
		StringBuffer sql = new StringBuffer();
		sql.append(" select t.* from t_user t where  t.mobile=? and t.user_type=?2");
		EntityManager em = super.getEntiyManager();
		Query query = em.createNativeQuery(sql.toString(), User.class);
		query.setParameter(1, mobile);
		query.setParameter(2, userType);
		List list = query.getResultList();

		Object outO;
		if (list == null || list.size() == 0) {
			outO = null;
		} else {
			outO = list.get(0);
		}
		em.close();
		return (User) outO;
	}

	@Override
	public User findByNameOrMobile(String name, int userType) throws Exception {
		StringBuffer sql = new StringBuffer();
		sql.append(" select t.* from t_user t where (t.user_name=?1 or mobile=?2) and t.user_type=?3");
		EntityManager em = super.getEntiyManager();
		Query query = em.createNativeQuery(sql.toString(), User.class);
		query.setParameter(1, name);
		query.setParameter(2, name);
		query.setParameter(3, userType);
		List list = query.getResultList();
		Object outO;
		if (list == null || list.size() == 0) {
			outO = null;
		} else {
			outO = list.get(0);
		}
		em.close();
		return (User) outO;
	}

	@Override
	public User findByName(String userName, int userType) throws Exception {
		StringBuffer sql = new StringBuffer();
		sql.append(" select t.* from t_user t where  t.user_name=? and t.user_type=?2");
		EntityManager em = super.getEntiyManager();
		Query query = em.createNativeQuery(sql.toString(), User.class);
		query.setParameter(1, userName);
		query.setParameter(2, userType);
		List list = query.getResultList();
		Object outO;
		if (list == null || list.size() == 0) {
			outO = null;
		} else {
			outO = list.get(0);
		}
		em.close();
		return (User) outO;
	}

	@Override
	public User findById(Integer Id, int userType) throws Exception {
		EntityManager em = super.getEntiyManager();
		Query query = em.createNativeQuery(" select t.* from t_user t where  t.id=?1 and t.user_type=?2", User.class);
		query.setParameter(1, Id);
		query.setParameter(2, userType);
		List list = query.getResultList();
		Object outO;
		if (list == null || list.size() == 0) {
			outO = null;
		} else {
			outO = list.get(0);
		}
		em.close();
		return (User) outO;
	}

	@Override
	public boolean updateUser(User user) throws Exception {
		StringBuffer sql = new StringBuffer();
		sql.append(" update t_user as t set t.user_type=").append(user.getUserType());
		if (user.readPassword() != null) {
			sql.append(", t.password = '").append(DigestUtils.md5DigestAsHex(user.readPassword().getBytes()))
					.append("'");
		}
		if (user.getMobile() != null) {
			sql.append(",t.mobile = '").append(user.getMobile()).append("'");
		}
		if (user.getGender() != null) {
			sql.append(", t.gender = '").append(user.getGender()).append("'");
		}
		if (user.getAddress() != null) {
			sql.append(", t.address = '").append(user.getAddress()).append("'");
		}
		if (user.getWxAccount() != null) {
			sql.append(", t.wx_account = '").append(user.getWxAccount()).append("'");
		}
		if (user.getZfbAccount() != null) {
			sql.append(", t.zfb_account = '").append(user.getZfbAccount()).append("'");
		}
		if (user.getBankAccount() != null) {
			sql.append(", t.bank_account = '").append(user.getBankAccount()).append("'");
		}
		if (user.getHospital() != null) {
			sql.append(", t.hospital = '").append(user.getHospital()).append("'");
		}
		if (user.getDept() != null) {
			sql.append(", t.dept = '").append(user.getDept()).append("'");
		}
		sql.append(" where t.id = ").append(user.getId());
		EntityManager em = this.getEntiyManager();
		em.getTransaction().begin();
		try {
			Query query = em.createNativeQuery(sql.toString());
			int i = query.executeUpdate();
			em.getTransaction().commit();
			return i > 0;
		} catch (Exception e) {
			em.getTransaction().rollback();
			throw e;
		} finally {
			em.close();
		}
	}

	@Override
	public boolean updateUserRegId(Integer userId, String mRegId) throws Exception {
		EntityManager em = super.getEntiyManager();
		try {
			em.getTransaction().begin();
			Query query = em.createNativeQuery(" update t_user as t set t.mregid= ?1 where id = ?2 ", User.class);
			query.setParameter(1, mRegId + "");
			query.setParameter(2, userId + "");
			int i = query.executeUpdate();
			em.getTransaction().commit();
			return i > 0;
		} catch (Exception e) {
			e.printStackTrace();
			em.getTransaction().rollback();
			throw e;
		} finally {
			em.close();
		}

	}

	@Override
	public List<User> findUser(Integer id, String name, String hospital, int i) throws Exception {
		StringBuffer sql = new StringBuffer();
		sql.append(" select t.* from t_user t where  t.user_type=  " + i);
		if (id != null) {
			sql.append(" and id = " + id);
		}
		if (!StringUtils.isEmpty(name)) {
			name = "'%" + name + "%'";
			sql.append(" and user_name like " + name + " or true_name like  " + name);
		}
		if (!StringUtils.isEmpty(hospital)) {
			hospital = "'%" + hospital + "%'";
			sql.append(" and hospital like " + hospital);
		}
		EntityManager em = super.getEntiyManager();
		Query query = em.createNativeQuery(sql.toString(), User.class);
		List<User> list = query.getResultList();
		em.close();
		return list;
	}
}
